# Xpath 
# 绝对路径 相对路径
# wd.find_element(By.XPATH,'') 
#//表示相对位置  /表示绝对位置
#  *通配符
# /*[@属性名='属性值']
# class属性有多个：xpath要写全所有的class ，css不用写全
# //p[2]  等价于 p:nth-of-tyep(2)
# //*[2]  第二个子元素
# //p[last()-1] 选取倒数第二个子元素
# //p[position()<=2]    -----  //*[@class='multi_choice']/*[position()>=last()-2]
# //option | //h4   等价于 option ,h4
# 选择父节点 /..   ----- //p/..
# 要在某在元素内部使用xpath  要在表示前面加个.  ---- './/p'


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from changliang import LUJING
from selenium.webdriver.chrome.service import Service
import time
import re

wd = webdriver.Chrome(service=Service(LUJING))
wd.implicitly_wait(5)

wd.get('https://kyfw.12306.cn/otn/leftTicket/init')

chufa = wd.find_element(By.CSS_SELECTOR,'#fromStationText')
chufa.click()
chufa.send_keys('南京南')
wd.find_element(By.CSS_SELECTOR,'.citylineover').click()

daoda = wd.find_element(By.CSS_SELECTOR,'#toStationText')
daoda.click()
daoda.send_keys('杭州东')
wd.find_element(By.CSS_SELECTOR,'.citylineover').click()

select_ele = wd.find_element(By.CSS_SELECTOR,'#cc_start_time')
s = Select(select_ele)
s.select_by_index(2)

wd.find_element(By.CSS_SELECTOR,'#date_range li:nth-of-type(2)').click()

# list1_ele = wd.find_elements(By.CSS_SELECTOR,'#queryLeftTable>tr[yp_info_new]')
# lei_che_erdengzuo = []
# for info in list1_ele:
#     er_deng_zuo = info.find_element(By.XPATH,'./td[5]').text
#     lei_che_hao = info.find_element(By.XPATH,'./td[5]').get_attribute('aria-label')
#     print(lei_che_hao)
#     if er_deng_zuo == '有':     
#         lei_che_hao = re.findall('^[0-9A-Za-z]*',lei_che_hao)
#         print(lei_che_hao)
#         lei_che_erdengzuo.append(lei_che_hao)

# for i in lei_che_erdengzuo:
#     print(i)


#这样直接一次性到位用xpath
lieche_eles = wd.find_elements(By.XPATH,'//*[@id="queryLeftTable"]//td[4][@class]/.. /preceding-sibling::*[1]')
for i in lieche_eles:
    leichehao11 = i.get_attribute('datatran')
    print(leichehao11)
time.sleep(3)